package cn.qingyun.gis.modules.init.utils;

import cn.qingyun.gis.exception.CommonException;
import lombok.extern.slf4j.Slf4j;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.io.WKTReader;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class GeometryTool {
    private static final WKTReader WKT_READER = new WKTReader();

    public static Geometry parse(String geometryString) {
        Geometry geometry;
        try {
            geometry = WKT_READER.read(geometryString);
            geometry.setSRID(4326);
        } catch (Exception e) {
            throw new CommonException("不能解析为地理数据类型, 请修改后重试");
        }
        return geometry;
    }

    public static boolean isValid(String wkt) {
        Geometry geo;
        try {
            geo = parse(wkt);
        } catch (Exception e) {
            return false;
        }
        return geo != null;
    }

}
